﻿using System;
// <copyright file="DomainProvince.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System.Linq;
    using System.Text;
    using LightSwitchApplication.Rules;
    using LightSwitchApplication.UserCode;

    public partial class DomainProvince
    {
        #region General Summaries

        partial void Region_Compute(ref string result)
        {
            if (this.BaseProvince != null)
            {
                result = this.BaseProvince.Region;
            }
        }

        partial void Name_Compute(ref string result)
        {
            if (this.BaseProvince != null)
            {
                result = this.BaseProvince.Name;
            }
        }

        partial void FullName_Compute(ref string result)
        {
            result = string.Format("{0} - {1}", this.Region, this.Name);
        }

        partial void GMLevelSummary_Compute(ref string result)
        {
            if (this.BaseProvince != null)
            {
                string fortString = (this.FortificationLevel > 0) ? string.Format("[{0}]", this.FortificationLevel) : string.Empty;
                result = string.Format("{0} ({1}{2}/{3})", this.BaseProvince.Name, this.PopulationLevel, fortString, this.MagicLevel);
            }
        }

        partial void PublicLevelSummary_Compute(ref string result)
        {
            if (this.BaseProvince != null)
            {
                string fortString = (this.FortificationLevel > 0) ? string.Format("[{0}]", Utilities.MaskValue(this.FortificationLevel)) : string.Empty;
                result = string.Format("{0} ({1}{2}/{3})", this.BaseProvince.Name, this.PopulationLevel, fortString, this.MagicLevel);
            }
        }

        #endregion

        #region TradeRoutes Computed

        partial void PublicNumberTradeRoutes_Compute(ref string result)
        {
            var trsStart = this.GuildHoldings
                                   .SelectMany(h => h.TradeRoutesStarting)
                                   .Where(tr => tr.Visibility == (int)VisibilityEnum.Public);

            var trsEnd = this.GuildHoldings
                                    .SelectMany(h => h.TradeRoutesEnding)
                                    .Where(tr => tr.Visibility == (int)VisibilityEnum.Public);

            result = Utilities.MaskValue(trsStart
                                                .Concat(trsEnd)
                                                .Distinct()
                                                .Count());
        }

        partial void OwnerNumberTradeRoutes_Compute(ref int result)
        {
            var trsStart = this.GuildHoldings
                                    .SelectMany(h => h.TradeRoutesStarting)
                                    .Where(tr => tr.Visibility == (int)VisibilityEnum.Public);

            var trsEnd = this.GuildHoldings
                                    .SelectMany(h => h.TradeRoutesEnding)
                                    .Where(tr => tr.Visibility == (int)VisibilityEnum.Public);

            result = trsStart
                        .Concat(trsEnd)
                        .Distinct()
                        .Count();
        }

        partial void GMNumberTradeRoutes_Compute(ref int result)
        {
            var trsStart = this.GuildHoldings
                                .SelectMany(h => h.TradeRoutesStarting);

            var trsEnd = this.GuildHoldings
                                .SelectMany(h => h.TradeRoutesEnding);

            result = trsStart
                        .Concat(trsEnd)
                        .Distinct()
                        .Count();
        }

        #endregion

        #region LeyLines Computed

        partial void GMNumberLeyLines_Compute(ref int result)
        {
            var llsStart = this.SourceHoldings
                                .SelectMany(h => h.LeyLinesStarting);

            var llsEnd = this.SourceHoldings
                                .SelectMany(h => h.LeyLinesEnding);

            result = llsStart
                        .Concat(llsEnd)
                        .Distinct()
                        .Count();
        }

        #endregion

        #region Armies

        partial void OwnerNumberArmies_Compute(ref int result)
        {
            result = this.Armies.Where(a => a.Visibility != (int)VisibilityEnum.GameMaster).Count();
        }

        partial void GMNumberArmies_Compute(ref int result)
        {
            result = this.Armies.Count();
        }

        #endregion

        #region GM Holdings Summaries

        partial void GMLawHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.LawHoldings)
            {
                builder.Append(holding.GMSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        partial void GMTempleHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.TempleHoldings)
            {
                builder.Append(holding.GMSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        partial void GMGuildHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.GuildHoldings)
            {
                builder.Append(holding.GMSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        partial void GMSourceHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.SourceHoldings)
            {
                builder.Append(holding.GMSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        #endregion

        #region Owner Holdings Summaries

        partial void OwnerLawHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.LawHoldings
                                        .Where(h => h.Visibility == (int)VisibilityEnum.Public ||
                                                   (h.Visibility == (int)VisibilityEnum.Owner && h.Owner == this.Domain)))
            {
                builder.Append(holding.GMSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        partial void OwnerTempleHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.TempleHoldings
                                        .Where(h => h.Visibility == (int)VisibilityEnum.Public ||
                                                   (h.Visibility == (int)VisibilityEnum.Owner && h.Owner == this.Domain)))
            {
                builder.Append(holding.GMSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        partial void OwnerGuildHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.GuildHoldings
                                        .Where(h => h.Visibility == (int)VisibilityEnum.Public ||
                                                   (h.Visibility == (int)VisibilityEnum.Owner && h.Owner == this.Domain)))
            {
                builder.Append(holding.GMSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        partial void OwnerSourceHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.SourceHoldings
                                        .Where(h => h.Visibility == (int)VisibilityEnum.Public ||
                                              (h.Visibility == (int)VisibilityEnum.Owner && h.Owner == this.Domain)))
            {
                builder.Append(holding.GMSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        #endregion

        #region Public Holdings Summaries

        partial void PublicLawHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.LawHoldings
                                        .Where(h => h.Visibility == (int)VisibilityEnum.Public))
            {
                builder.Append(holding.PublicSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        partial void PublicTempleHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.TempleHoldings
                                        .Where(h => h.Visibility == (int)VisibilityEnum.Public))
            {
                builder.Append(holding.PublicSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        partial void PublicGuildHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.GuildHoldings
                                        .Where(h => h.Visibility == (int)VisibilityEnum.Public))
            {
                builder.Append(holding.PublicSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        partial void PublicSourceHoldingsSummary_Compute(ref string result)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var holding in this.SourceHoldings
                                        .Where(h => h.Visibility == (int)VisibilityEnum.Public))
            {
                builder.Append(holding.PublicSummary);
                builder.Append("/");
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            result = builder.ToString();
        }

        #endregion
        
        #region HasInformation

        partial void HasPublicInformation_Compute(ref bool result)
        {
            result = !string.IsNullOrEmpty(this.PublicInformation);
        }

        partial void HasGMInformation_Compute(ref bool result)
        {
            result = !string.IsNullOrEmpty(this.GMInformation);
        }

        partial void HasOwnerInformation_Compute(ref bool result)
        {
            result = !string.IsNullOrEmpty(this.OwnerInformation);
        }

        #endregion

        partial void MagicLevel_Compute(ref int result)
        {
            result = Calculators.DomainProvinceCalculator.CalculateMagicLevel(this);
        }
    }
}
